﻿using System;
using System.ComponentModel;
using iTeamyou.LTS.Core.Services;
using iTeamyou.LTS.Core.Utils;
using Microsoft.EntityFrameworkCore;
using static System.Formats.Asn1.AsnWriter;

namespace iTeamyou.LTS.Core.Models
{
	public class MainDBContext : DbContext
	{
        public MainDBContext()
        { }

        public MainDBContext(DbContextOptions<MainDBContext> options) : base(options)
		{ }

		public DbSet<Supplier> Suppliers { get; set; }
		public DbSet<SupplierLicense> SupplierLicenses { get; set; }
		public DbSet<SupplierLicenseAttachment> SupplierLicenseAttachments { get; set; }

		public DbSet<Product> Products { get; set; }
		public DbSet<ProductLicense> ProductLicenses { get; set; }
		public DbSet<ProductLicenseAttachment> ProductLicenseAttachments { get; set; }

		public DbSet<Manufacturer> Manufacturers { get; set; }
        public DbSet<ManufacturerLicense> ManufacturerLicenses { get; set; }
        public DbSet<ManufacturerLicenseAttachment> ManufacturerLicenseAttachments { get; set; }

        public DbSet<Batch> Batches { get; set; }
        public DbSet<BatchLicense> BatchLicenses { get; set; }
        public DbSet<BatchLicenseAttachment> BatchLicenseAttachments { get; set; }

        //public DbSet<InboundProduct> InboundProducts { get; set; }

        public DbSet<LicenseType> LicenseTypes { get; set; }
        public DbSet<ProductHierarchy> ProductHierarchies { get; set; }

        public DbSet<SyncBarcode> SyncBarcodes { get; set; }
        public DbSet<SyncBatch> SyncBatches { get; set; }
        public DbSet<SyncProduct> SyncProducts { get; set; }
        public DbSet<SyncSupplier> SyncSuppliers { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<LoginApp>().HasNoKey();
            builder.Entity<LoginUser>().HasKey(t => new { t.Id, t.AppCode });
            builder.Entity<LicenseType>().HasKey(t => new { t.TypeId, t.Module });
            builder.Entity<ProductHierarchy>().HasKey(t => new { t.Id });

            builder.Entity<Supplier>().HasKey(t => t.Id);
            builder.Entity<SupplierLicense>().HasKey(t => t.Id);
            builder.Entity<SupplierLicenseAttachment>().HasKey(t => new { t.Id, t.LicenseId });
            builder.Entity<Product>().HasKey(t => t.Id);
            builder.Entity<ProductLicense>().HasKey(t => t.Id);
            builder.Entity<ProductLicenseAttachment>().HasKey(t => new { t.Id, t.LicenseId });
            builder.Entity<Manufacturer>().HasKey(t => t.Id);
            builder.Entity<ManufacturerLicense>().HasKey(t => t.Id);
            builder.Entity<ManufacturerLicenseAttachment>().HasKey(t => new { t.Id, t.LicenseId });
            builder.Entity<Batch>().HasKey(t => t.Id);
            builder.Entity<BatchLicense>().HasKey(t => t.Id);
            builder.Entity<BatchLicenseAttachment>().HasKey(t => new { t.Id, t.LicenseId });

            //builder.Entity<SyncBarcode>().HasNoKey();
            //builder.Entity<SyncBatch>().HasNoKey();
            //builder.Entity<SyncProduct>().HasNoKey();
            //builder.Entity<SyncSupplier>().HasNoKey();

            builder.Entity<SyncBarcode>().HasKey(t => t.SyncId);
            builder.Entity<SyncBatch>().HasKey(t => t.SyncId);
            builder.Entity<SyncProduct>().HasKey(t => t.SyncId);
            builder.Entity<SyncSupplier>().HasKey(t => t.SyncId);

            base.OnModelCreating(builder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                var shareService = new SharedService();
                //optionsBuilder.UseMySql("server=localhost; userid=root; password=111qqq!; port=3306; database=lts; sslmode=Required;", new MySqlServerVersion(new Version()));
                optionsBuilder.UseMySql(shareService.GetConnectionString("MainDB"), new MySqlServerVersion(new Version()));
            }
        }
    }
}

